package com.lemeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lemeng.entity.Result;
import com.lemeng.pojo.Address;
import com.lemeng.pojo.Order;
import com.lemeng.service.AddressService;
import com.lemeng.service.CartService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("cart")
public class CartController {

    @Reference
    CartService cartService;
    @Reference
    AddressService addressService;

    @RequestMapping("addCart")
    public Result addCart(String skuId, Integer num){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();

        cartService.addCart(name,skuId,num);

        return new Result();
    }

    @RequestMapping("findCart")
    public List<Map> findCart(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
         return   cartService.findCart(name);
    }


    @RequestMapping("changeCheckedState")
    public Result changeCheckedState(String ids){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.changeCheckedState(name,ids);
        return new Result();
    }

    @RequestMapping("delAll")
    public Result delAll(String ids){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.delAll(name,ids);
        return new Result();
    }

    @RequestMapping("getMyPreMoney")
    public Integer getMyPreMoney(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return   cartService.getMyPreMoney(name);
    }

    @RequestMapping("submitOrder")
    public Map<String,Object> submitOrder(@RequestBody Order order){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return cartService.submitOrder(name,order);
    }

    @RequestMapping("findMyAddress")
    public List<Address> findMyAddress(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();

        Map<String, Object> map = Collections.singletonMap("username", name);
        return   addressService.findList(map);
    }
}
